import {rulesselectall,deleterule} from '../../../server/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hdname:'',
    list:[],
    pageNo:1,
    pageSize:10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      this.setData({list:[],pageNo:1})
      this.getdata()
     
  },
  //获取列表数据
  async getdata(val){
    const params = {
      rulesname:this.data.hdname,
      pageSize:val == undefined ? this.data.pageSize : val.pageSize,
      pageNo:val == undefined ? this.data.pageNo : val.pageNo,
    }
    const res = await rulesselectall(params)
    if(res.code == 200)
    {
      const source = res.result.records
      const arr = source.map((item)=>{
        item['time'] =  this.captureTime(item.createTime)
        return item
      })
      this.setData({list:[...this.data.list,...arr]})
    }
  },
  captureTime(time){
    var timeArr = time.replace(" ", ":").replace(/\:/g, "-").split("-");
    var nian = timeArr[0];
    var yue = timeArr[1];
    var ri = timeArr[2];
    var shi = timeArr[3];
    var fen = timeArr[4];
    var miao = timeArr[5];
    const year = nian + '/' + yue + '/' + ri 
    console.log(year);
    return year
},
getMore(){
  this.data.pageNo++ 
  const params = {
    pageNo:this.data.pageNo,
    pageSize:this.data.pageSize
  }
  this.getdata(params)

},
  //搜索
  searchdata(){
    console.log(this.data.hdname);
    this.setData({list:[],pageNo:1})
    this.getdata()
  },
  //添加
  add(){
    wx.navigateTo({
      url: `../../../subPackagesB/pages/addrule/addrule`,
    })
  },
  //编辑
  edit(e){
      const obj = e.currentTarget.dataset.obj
      wx.navigateTo({
        url: `../../../subPackagesB/pages/addrule/addrule?obj=${JSON.stringify(obj)}`,
      })
  },
  //删除
  del(e){
    const id = e.currentTarget.dataset.id
    console.log(id,'ee');
    const that = this
    wx.showModal({
      title: '确定要删除吗?',
     async success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          const data = await deleterule({id:id})
          if(data.code == 200)
          {
            wx.showToast({
              title: '删除成功',
              icon:'success'
            })

            that.getdata()
            that.setData({list:[]})
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  sub(name){
    return name.substring(0,10)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})